<?php
/**
 * Created by PhpStorm.
 * User: EDZ
 * Date: 2020/6/22
 * Time: 17:27
 */

namespace utils;


class Integer
{
    private static function toStringByRadix($n, $radix, $digits) {
        $isPos = 1;
        if ($n < 0) {
            $n = 0 - $n;
            $isPos = 0;
        }
        $s = "";
        while (0 < $n) {
            $s = $digits[$n % $radix] . $s;
            $n = floor($n / $radix);
        }
        if (!$isPos) {
            $s = "-".$s;
        }
        return $s;
    }
    /**
     * @param $n int
     * @return string
     */
    public static function toHexString($n) {
        return self::toStringByRadix($n, 16, "0123456789ABCDEF");
    }

    public static function toBinaryString($n) {
        return self::toStringByRadix($n, 2, "01");
    }

    public static function toOctalString($n) {
        return self::toStringByRadix($n, 8, "01234567");
    }
}